package club.zbiao.modules.system.rest;

import club.zbiao.aop.log.Log;
import club.zbiao.config.DataScope;
import club.zbiao.exception.BadRequestException;
import club.zbiao.modules.system.domain.Job;
import club.zbiao.modules.system.service.JobService;
import club.zbiao.modules.system.service.dto.JobDTO;
import club.zbiao.modules.system.service.query.JobQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author jie
 * @date 2019-03-29
 */
@RestController
@RequestMapping("api")
public class JobController {

    private static final String ENTITY_NAME = "job";
    @Autowired
    private JobService jobService;
    @Autowired
    private JobQueryService jobQueryService;
    @Autowired
    private DataScope dataScope;

    @Log("查询岗位")
    @GetMapping(value = "/job")
    @PreAuthorize("hasAnyRole('ADMIN','USERJOB_ALL','USERJOB_SELECT','USER_ALL','USER_SELECT')")
    public ResponseEntity getJobs(@RequestParam(required = false) String name,
                                  @RequestParam(required = false) Long deptId,
                                  @RequestParam(required = false) Boolean enabled,
                                  Pageable pageable) {
        // 数据权限
        Set<Long> deptIds = dataScope.getDeptIds();
        JobDTO jobDTO = new JobDTO();
        jobDTO.setName(name);
        jobDTO.setEnabled(enabled);
        jobDTO.setDeptIds(deptIds);
        jobDTO.setDeptId(deptId);
        return new ResponseEntity(jobQueryService.queryAll(jobDTO, pageable), HttpStatus.OK);
    }

    @Log("新增岗位")
    @PostMapping(value = "/job")
    @PreAuthorize("hasAnyRole('ADMIN','USERJOB_ALL','USERJOB_CREATE')")
    public ResponseEntity create(@Validated @RequestBody Job resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return new ResponseEntity(jobService.create(resources), HttpStatus.CREATED);
    }

    @Log("修改岗位")
    @PutMapping(value = "/job")
    @PreAuthorize("hasAnyRole('ADMIN','USERJOB_ALL','USERJOB_EDIT')")
    public ResponseEntity update(@Validated(Job.Update.class) @RequestBody Job resources) {
        jobService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除岗位")
    @DeleteMapping(value = "/job/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','USERJOB_ALL','USERJOB_DELETE')")
    public ResponseEntity delete(@PathVariable Long id) {
        jobService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}